package com.sitanyun.merchant.guide.frament.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.adapter.ProductAdapter;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.bean.ProductBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 产品
 */
public class ProductActivity extends BaseActivity {
    @BindView(R.id.rv_product)
    RecyclerView rvProduct;
    @BindView(R.id.sw_product)
    SwipeRefreshLayout swProduct;
    @BindView(R.id.smrz_msg)
    TextView smrzMsg;
    @BindView(R.id.ll_one)
    LinearLayout llOne;
    @BindView(R.id.ll_todaytwo)
    LinearLayout llTodaytwo;

    private ProductAdapter productAdapter;
    // 分页
    private int offset = 1;
    private List<ProductBean.DataBean.SceneListBean.RecordsBean> orderbeanlist;

    @Override
    public int getLayoutId() {
        return R.layout.activity_product;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        setTitleStr("产品许可");
        //创建布局管理
        orderbeanlist = new ArrayList<>();
        rvProduct.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        LinearLayoutManager layoutManager = new LinearLayoutManager(ProductActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvProduct.setLayoutManager(layoutManager);
        productAdapter = new ProductAdapter(ProductActivity.this);
        rvProduct.setAdapter(productAdapter);
        productAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        offset = 1;
        initokhttps();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (productAdapter != null) {
            productAdapter.cancelAllTimers();
        }
    }

    //请求数据
    private void initokhttps() {
        OkHttpUtils.get().url(Urls.sceneList)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .addParams("current", String.valueOf(offset))
                .addParams("size", "10")
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {
                swProduct.setRefreshing(false);
            }

            @Override
            public void onResponse(String response, int id) {
                ProductBean enterBean = new Gson().fromJson(response, ProductBean.class);
                swProduct.setRefreshing(false);
                if (enterBean.getCode() == 0) {
                    smrzMsg.setText("产品购买或续费请联系：" + enterBean.getData().getSaleName() + " " + enterBean.getData().getSalePhone());
                    if (offset == 1) {
                        if (enterBean.getData().getSceneList().getRecords().size() == 0) {
                            llOne.setVisibility(View.GONE);
                            llTodaytwo.setVisibility(View.VISIBLE);
                            return;
                        } else {
                            llOne.setVisibility(View.VISIBLE);
                            llTodaytwo.setVisibility(View.GONE);
                        }

                        orderbeanlist.clear();
                        productAdapter.setNewData(enterBean.getData().getSceneList().getRecords());
                        orderbeanlist.addAll(enterBean.getData().getSceneList().getRecords());
                        productAdapter.notifyDataSetChanged();
                    } else {
                        //orderbeanlist.addAll(orderBean.getData());
                        //底部没有数据时
                        if (enterBean.getData().getSceneList().getRecords().isEmpty()) {
                            productAdapter.loadMoreEnd();
                        } else {
                            //滑动加载数据
                            List<ProductBean.DataBean.SceneListBean.RecordsBean> data1 = enterBean.getData().getSceneList().getRecords();
                            orderbeanlist.addAll(data1);
                            productAdapter.addData(data1);
                            productAdapter.notifyDataSetChanged();
                            productAdapter.loadMoreComplete();

                        }

                    }
                } else {
                    ToastUtil.showToast(ProductActivity.this, enterBean.getMsg() + "");
                }

            }
        });
    }

    @Override
    public void initView() {
        //刷新
        swProduct.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                offset = 1;
                initokhttps();
            }
        });
        //加载更多
        productAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                offset++;
                initokhttps();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
